/*
 * @Author: zxc
 * @Date: 2020-09-11 16:16:28
 * @LastEditTime: 2020-09-11 16:22:50
 * @LastEditors: zxc
 * @Description: 防抖截流
 */
import { useEffect, useRef, useCallback,DependencyList } from "react";

export const useDebounce = (fn, delay, dep = []) => {
  const { current } = useRef({ fn, timer:null });
  useEffect(
    function () {
      current.fn = fn;
    },
    [fn]
  );

  return useCallback(function f(...args) {
    if (current.timer) {
      clearTimeout(current.timer);
    }
    current.timer = setTimeout(() => {
      current.fn.call(this, ...args);
    }, delay);
  }, dep);
};

